package com.yyaccp.appinfo.utils;

import com.yyaccp.appinfo.domain.AppInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ExcelUtils {
    public static void export(List<AppInfo> dataList, HttpServletResponse response) throws Exception {
        Workbook wb=new HSSFWorkbook();//新建一个工作簿
        /*
         * 导出所以用到输出流
         *参数为输出的地址
         */
//        FileOutputStream fout=new FileOutputStream("F:\\poi.xls");
        response.setContentType("octets/stream");
        response.addHeader("Content-Disposition", "attachment;filename="+System.currentTimeMillis()+".xls");
        ServletOutputStream fout = response.getOutputStream();
        Sheet sheet= wb.createSheet("第一个sheet页");//创建一个sheet页
        for (int i = 0; i < dataList.size(); i++) {
            AppInfo appInfo = dataList.get(i);
            Row row=sheet.createRow(i); // 创建一个行 第一行
            int j = 0;
            row.createCell(j++).setCellValue(appInfo.getId());
            row.createCell(j++).setCellValue(appInfo.getSoftwareName());
            row.createCell(j++).setCellValue(appInfo.getAPKName());
            row.createCell(j++).setCellValue(appInfo.getAppInfo());
            row.createCell(j++).setCellValue(appInfo.getCreationDate());
            row.createCell(j++).setCellValue(appInfo.getStatus());
        }


        wb.write(fout);//Workbook提供了write的方法
        fout.close();//将输出流关闭

    }
}
